package com.thememaker.lockstyle.control;

import com.alibaba.fastjson.JSONObject;
import com.jfoenix.controls.JFXRadioButton;
import com.thememake.ui.Toast;
import com.thememaker.lockstyle.LockstyleMainController;
import com.thememaker.lockstyle.LockstyleShowUtils;
import com.thememaker.lockstyle.bean.animation.Position;
import com.thememaker.lockstyle.bean.command.EndPoint;
import com.thememaker.lockstyle.bean.command.StartPoint;
import com.thememaker.lockstyle.bean.ui.BaseView;
import com.thememaker.lockstyle.bean.ui.Group;
import com.thememaker.lockstyle.bean.ui.Image;
import com.thememaker.lockstyle.bean.ui.Unlocker;
import com.thememaker.lockstyle.parsii.ExpressionUtils;
import com.thememaker.lockstyle.parsii.LockscreenViewPaesiiUtils;
import com.thememaker.util.SVGLoadUtils;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

import java.nio.file.Path;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;

public class UnlockerControl implements ILockscreenControl {
    private LockstyleShowUtils lockstyleShowUtils;
    private LinkedHashMap<String, BaseView> lockscreenMap;
    private VBox vBox;
    private Unlocker baseView;
    private Path lockscreenPath;
    private ExpressionUtils expressionUtils;
    private Consumer<BaseView> consumer;

    private TextField spX, spY, spW, spH, epX, epY, epW, epH, epL;
    private JFXRadioButton radio1, radio2, radio3, radio4;
    private HBox unlockTitleHBox;
    private Button addRelaBtn;

    private static final String UNLOCK_UP = "up";
    private static final String UNLOCK_RIGHT = "right";
    private static final String UNLOCK_DOWN = "down";
    private static final String UNLOCK_LEFT = "left";
    private String type = UNLOCK_UP;

    private boolean isSave = false;
    private boolean isEditRele = false;

    private LinkedHashMap<String, BaseView> imageAndGroupMap = new LinkedHashMap<>();


    public UnlockerControl(LockstyleShowUtils lockstyleShowUtils) {
        this.lockstyleShowUtils = lockstyleShowUtils;
        lockscreenMap = lockstyleShowUtils.getLockscreen().getLockScreenViewMap();
        lockscreenMap.keySet().forEach(key -> {
            if (lockscreenMap.get(key) instanceof Image) {
                if (!"bg".equals(lockscreenMap.get(key).getName())) {
                    imageAndGroupMap.put(key, lockscreenMap.get(key));
                }
            } else if (lockscreenMap.get(key) instanceof Group) {
                imageAndGroupMap.put(key, lockscreenMap.get(key));
            }
        });
    }

    @Override
    public Node initLockscreenControl(BaseView baseView, Path lockscreenPath, ExpressionUtils expressionUtils, Consumer<BaseView> consumer) {
        this.baseView = (Unlocker) baseView;
        this.lockscreenPath = lockscreenPath;
        this.expressionUtils = expressionUtils;
        this.consumer = consumer;
        vBox = LayoutComponentUtils.getInstance().getControlView(670, 600);
        unlockTitleHBox = new HBox();
        vBox.getChildren().add(unlockTitleHBox);
        vBox.getChildren().add(getUnlockDirectionNode());
        vBox.getChildren().add(LayoutComponentUtils.getInstance().getControlTitleLabel("起始区域"));
        vBox.getChildren().add(getStartPointNode());
        vBox.getChildren().add(LayoutComponentUtils.getInstance().getControlTitleLabel("结束区域"));
        vBox.getChildren().add(getEndPointNode());
        vBox.getChildren().add(LayoutComponentUtils.getInstance().getControlTitleLabel("解锁距离"));
        vBox.getChildren().add(getUnlockLengthNode());
        updateTitleHBox();
        HBox relaTitleHBox = new HBox();
        addRelaBtn = new Button("添加关联");
        addRelaBtn.setId("diy-button");
        addRelaBtn.setGraphic(SVGLoadUtils.getInstance().getMagnetSVG(10, LockstyleMainController.buttonColor));
        HBox.setMargin(addRelaBtn, new Insets(0, 0, 0, 10));
        relaTitleHBox.getChildren().addAll(LayoutComponentUtils.getInstance().getControlTitleLabel("关联控件"), addRelaBtn);
        vBox.getChildren().add(relaTitleHBox);
        vBox.getChildren().add(getMoveRelaNode());
        addRadioEvent();
        addFieldEvent();
        addReleEvent();
        return vBox;
    }

    private void updateTitleHBox() {
        if (this.baseView == null) {
            newUnlockerObj();
            unlockTitleHBox.getChildren().clear();
            Button newSaveBtn = new Button("保存");
            newSaveBtn.setId("diy-button");
            newSaveBtn.setGraphic(SVGLoadUtils.getInstance().getSaveSVG(12, LockstyleMainController.buttonColor));
            HBox.setMargin(newSaveBtn, new Insets(0, 20, 0, 20));
            unlockTitleHBox.getChildren().addAll(LayoutComponentUtils.getInstance().getControlTitleLabel("解锁控件"), newSaveBtn);
            newSaveBtn.setOnAction(event -> {
                if (saveInfo2Xml()) {
                    idDisable(true);
                }
            });
        } else {
            unlockTitleHBox.getChildren().clear();
            unlockTitleHBox.getChildren().addAll(LayoutComponentUtils.getInstance().getEditTitleHbox("解锁控件",
                    (editButton, saveButton) -> {
                        idDisable(false);
                        editButton.setVisible(false);
                        saveButton.setVisible(true);
                    },
                    (editButton, saveButton) -> {
                        if (saveInfo2Xml()) {
                            idDisable(true);
                            editButton.setVisible(true);
                            saveButton.setVisible(false);
                        }
                    }));
            addFieldText();
            idDisable(true);
        }
    }

    private void newUnlockerObj() {
        this.baseView = new Unlocker();
        baseView.setName("unlocker");
        baseView.setAlwaysShow("true");
        baseView.setBounceInitSpeed("150");
        baseView.setBounceAccelation("1500");
        StartPoint startPoint = new StartPoint();
        EndPoint endpoint = new EndPoint();
        com.thememaker.lockstyle.bean.command.Path path = new com.thememaker.lockstyle.bean.command.Path();
        List<Position> positionList = new ArrayList<>();
        for (int i = 0; i < 2; i++) {
            Position position = new Position();
            position.setX("0");
            position.setY("0");
            positionList.add(position);
        }
        endpoint.setPath(path);
        path.setPositionList(positionList);
        baseView.setStartPoint(startPoint);
        baseView.setEndPoint(endpoint);
    }

    private void addReleEvent() {
        addMoveLabel.setVisible(isEditRele);
        addMoveNode.setVisible(isEditRele);
        addRelaBtn.setOnAction(event -> {
            if (isEditRele) {
                isEditRele = false;
                addRelaBtn.setText("添加关联");
                addMoveLabel.setVisible(isEditRele);
                addMoveNode.setVisible(isEditRele);
                imageAndGroupMap.keySet().forEach(key -> {
                    if (imageAndGroupMap.get(key).getUnlockMoveFX()) {
                        if (!imageAndGroupMap.get(key).getX().contains("#unlocker.move_x")) {
                            imageAndGroupMap.get(key).setX("#unlocker.move_x+" + Optional.ofNullable(imageAndGroupMap.get(key).getX()).orElse(""));
                        }
                        if (!imageAndGroupMap.get(key).getY().contains("#unlocker.move_y")) {
                            imageAndGroupMap.get(key).setY("#unlocker.move_y+" + Optional.ofNullable(imageAndGroupMap.get(key).getY()).orElse(""));
                        }
                    } else {
                        if (imageAndGroupMap.get(key).getX().contains("#unlocker.move_x+") || imageAndGroupMap.get(key).getY().contains("#unlocker.move_y+")) {
                            imageAndGroupMap.get(key).setX(Optional.ofNullable(imageAndGroupMap.get(key).getX()).map(x -> x.replace("#unlocker.move_x+", "")).orElse(""));
                            imageAndGroupMap.get(key).setY(Optional.ofNullable(imageAndGroupMap.get(key).getY()).map(y -> y.replace("#unlocker.move_y+", "")).orElse(""));
                        } else if (imageAndGroupMap.get(key).getX().contains("#unlocker.move_x-") || imageAndGroupMap.get(key).getY().contains("#unlocker.move_y-")){
                            imageAndGroupMap.get(key).setX(Optional.ofNullable(imageAndGroupMap.get(key).getX()).map(x -> x.replace("#unlocker.move_x-", "")).orElse(""));
                            imageAndGroupMap.get(key).setY(Optional.ofNullable(imageAndGroupMap.get(key).getY()).map(y -> y.replace("#unlocker.move_y-", "")).orElse(""));
                        }

                    }
                    lockscreenMap.put(key, imageAndGroupMap.get(key));
                    consumer.accept(imageAndGroupMap.get(key));
                });
            } else {
                isEditRele = true;
                addRelaBtn.setText("确定关联");
                addMoveLabel.setVisible(isEditRele);
                addMoveNode.setVisible(isEditRele);
            }
            updateAddGetMoveNode();
        });
    }


    private void idDisable(boolean disable) {
        radio1.setDisable(disable);
        radio2.setDisable(disable);
        radio3.setDisable(disable);
        radio4.setDisable(disable);
        spX.setEditable(!disable);
        spY.setEditable(!disable);
        spW.setEditable(!disable);
        spH.setEditable(!disable);
        epX.setEditable(!disable);
        epY.setEditable(!disable);
        epW.setEditable(!disable);
        epH.setEditable(!disable);
        epL.setEditable(!disable);
    }

    private boolean saveInfo2Xml() {
        if (isSave) {
            switch (type) {
                case UNLOCK_UP:
                    if (getXMLNum(spY.getText()) - getXMLNum(epL.getText()) - getXMLNum(epH.getText()) != getXMLNum(epY.getText())) {
                        Toast.makeText(new Stage(), "上划解锁不正确", 2000);
                        spY.setId("textfield-error");
                        epL.setId("textfield-error");
                        epH.setId("textfield-error");
                        epY.setId("textfield-error");
                        return false;
                    }
                    spY.setId("diy-textfield");
                    epL.setId("diy-textfield");
                    epH.setId("diy-textfield");
                    epY.setId("diy-textfield");
                    baseView.getEndPoint().getPath().getPositionList().get(1).setY("-" + epL.getText());
                    baseView.getEndPoint().getPath().getPositionList().get(1).setX("0");
                    break;
                case UNLOCK_DOWN:
                    if (getXMLNum(spY.getText()) + getXMLNum(epL.getText()) != getXMLNum(epY.getText())) {
                        Toast.makeText(new Stage(), "下划解锁不正确", 2000);
                        spY.setId("textfield-error");
                        epL.setId("textfield-error");
                        epY.setId("textfield-error");
                        return false;
                    }
                    spY.setId("diy-textfield");
                    epL.setId("diy-textfield");
                    epY.setId("diy-textfield");
                    baseView.getEndPoint().getPath().getPositionList().get(1).setY(epL.getText());
                    baseView.getEndPoint().getPath().getPositionList().get(1).setX("0");
                    break;
                case UNLOCK_LEFT:
                    if (getXMLNum(spX.getText()) - getXMLNum(epL.getText()) - getXMLNum(epW.getText()) != getXMLNum(epX.getText())) {
                        Toast.makeText(new Stage(), "左划解锁不正确", 2000);
                        spX.setId("textfield-error");
                        epL.setId("textfield-error");
                        epW.setId("textfield-error");
                        epX.setId("textfield-error");
                        return false;
                    }
                    spX.setId("diy-textfield");
                    epL.setId("diy-textfield");
                    epW.setId("diy-textfield");
                    epX.setId("diy-textfield");
                    baseView.getEndPoint().getPath().getPositionList().get(1).setX("-" + epL.getText());
                    baseView.getEndPoint().getPath().getPositionList().get(1).setY("0");
                    break;
                case UNLOCK_RIGHT:
                    if (getXMLNum(spX.getText()) + getXMLNum(epL.getText()) != getXMLNum(epX.getText())) {
                        Toast.makeText(new Stage(), "右划解锁不正确", 2000);
                        spX.setId("textfield-error");
                        epL.setId("textfield-error");
                        epX.setId("textfield-error");
                        return false;
                    }
                    spX.setId("diy-textfield");
                    epL.setId("diy-textfield");
                    epX.setId("diy-textfield");
                    baseView.getEndPoint().getPath().getPositionList().get(1).setX(epL.getText());
                    baseView.getEndPoint().getPath().getPositionList().get(1).setY("0");
                    break;
                default:
                    break;
            }
            baseView.getStartPoint().setX(spX.getText());
            baseView.getStartPoint().setY(spY.getText());
            baseView.getStartPoint().setW(spW.getText());
            baseView.getStartPoint().setH(spH.getText());
            baseView.getEndPoint().setX(epX.getText());
            baseView.getEndPoint().setY(epY.getText());
            baseView.getEndPoint().setW(epW.getText());
            baseView.getEndPoint().setH(epH.getText());
            baseView.getEndPoint().getPath().setX(spX.getText());
            baseView.getEndPoint().getPath().setY(spY.getText());
            baseView.getEndPoint().getPath().setW(spW.getText());
            baseView.getEndPoint().getPath().setH(spH.getText());
            System.out.println("saveInfo2Xml >>> " + JSONObject.toJSON(baseView));
            updateTitleHBox();
            consumer.accept(baseView);
            return true;
        } else {
            Toast.makeText(new Stage(), "配置不正确,无法保存.", 2000);
            return false;

        }
    }

    private Node getUnlockDirectionNode() {
        HBox hBox = new HBox();
        ToggleGroup toggleGroup = new ToggleGroup();
        radio1 = LayoutComponentUtils.getInstance().getControlRadioButton("上滑解锁 ", 100, toggleGroup, true);
        radio2 = LayoutComponentUtils.getInstance().getControlRadioButton("右滑解锁 ", 100, toggleGroup, false);
        radio3 = LayoutComponentUtils.getInstance().getControlRadioButton("下滑解锁 ", 100, toggleGroup, false);
        radio4 = LayoutComponentUtils.getInstance().getControlRadioButton("左滑解锁 ", 100, toggleGroup, false);
        hBox.getChildren().addAll(LayoutComponentUtils.getInstance().getControlNormalLabel("解锁方向　"), radio1, radio2, radio3, radio4);
        return hBox;
    }

    private Node getStartPointNode() {
        VBox vBox = new VBox();
        HBox hBox0 = new HBox();
        hBox0.setAlignment(Pos.CENTER_LEFT);
        HBox hBox1 = new HBox();
        hBox1.setAlignment(Pos.CENTER_LEFT);
        Label label0 = LayoutComponentUtils.getInstance().getControlNormalLabel("X坐标: ");
        label0.setPrefWidth(50);
        Label label1 = LayoutComponentUtils.getInstance().getControlNormalLabel("Y坐标: ");
        label1.setPrefWidth(50);
        Label label2 = LayoutComponentUtils.getInstance().getControlNormalLabel("W宽度: ");
        label2.setPrefWidth(50);
        Label label3 = LayoutComponentUtils.getInstance().getControlNormalLabel("H高度: ");
        label3.setPrefWidth(50);
        spX = LayoutComponentUtils.getInstance().getControlTextField("", 200, true);
        spY = LayoutComponentUtils.getInstance().getControlTextField("", 200, true);
        spW = LayoutComponentUtils.getInstance().getControlTextField("", 200, true);
        spH = LayoutComponentUtils.getInstance().getControlTextField("", 200, true);
        hBox0.getChildren().addAll(label0, spX, label1, spY);
        hBox1.getChildren().addAll(label2, spW, label3, spH);
        VBox.setMargin(hBox0, new Insets(0, 0, 5, 0));
        VBox.setMargin(hBox1, new Insets(5, 0, 0, 0));
        vBox.getChildren().addAll(hBox0, hBox1);
        return vBox;
    }

    private Node getEndPointNode() {
        VBox vBox = new VBox();
        HBox hBox0 = new HBox();
        hBox0.setAlignment(Pos.CENTER_LEFT);
        HBox hBox1 = new HBox();
        hBox1.setAlignment(Pos.CENTER_LEFT);
        Label label0 = LayoutComponentUtils.getInstance().getControlNormalLabel("X坐标: ");
        label0.setPrefWidth(50);
        Label label1 = LayoutComponentUtils.getInstance().getControlNormalLabel("Y坐标: ");
        label1.setPrefWidth(50);
        Label label2 = LayoutComponentUtils.getInstance().getControlNormalLabel("W宽度: ");
        label2.setPrefWidth(50);
        Label label3 = LayoutComponentUtils.getInstance().getControlNormalLabel("H高度: ");
        label3.setPrefWidth(50);
        epX = LayoutComponentUtils.getInstance().getControlTextField("", 200, true);
        epY = LayoutComponentUtils.getInstance().getControlTextField("", 200, true);
        epW = LayoutComponentUtils.getInstance().getControlTextField("", 200, true);
        epH = LayoutComponentUtils.getInstance().getControlTextField("", 200, true);
        hBox0.getChildren().addAll(label0, epX, label1, epY);
        hBox1.getChildren().addAll(label2, epW, label3, epH);
        VBox.setMargin(hBox0, new Insets(0, 0, 5, 0));
        VBox.setMargin(hBox1, new Insets(5, 0, 0, 0));
        vBox.getChildren().addAll(hBox0, hBox1);
        return vBox;
    }

    private Node getUnlockLengthNode() {
        HBox hBox = new HBox();
        hBox.setAlignment(Pos.CENTER_LEFT);
        Label label = LayoutComponentUtils.getInstance().getControlNormalLabel("L距离: ");
        label.setPrefWidth(50);
        epL = LayoutComponentUtils.getInstance().getControlTextField("", 450, true);
        hBox.getChildren().addAll(label, epL);
        return hBox;
    }


    private HBox getMoveNode = new HBox();
    private HBox addMoveNode = new HBox();
    private Label addMoveLabel = LayoutComponentUtils.getInstance().getControlTitleLabel("未关联组件");
    private Label getMoveLabel = LayoutComponentUtils.getInstance().getControlTitleLabel("已关联组件");

    private Node getMoveRelaNode() {
        addMoveNode.getChildren().clear();
        getMoveNode.getChildren().clear();
        imageAndGroupMap.keySet().forEach(key -> {
            if (imageAndGroupMap.get(key) instanceof Image) {
                Image image = (Image) imageAndGroupMap.get(key);
                if (image.getUnlockMoveFX()) {
                    getMoveNode.getChildren().add(mapImageAddNode(image, true));
                } else {
                    addMoveNode.getChildren().add(mapImageAddNode(image, false));
                }
            } else if (imageAndGroupMap.get(key) instanceof Group) {
                Group group = (Group) imageAndGroupMap.get(key);
                if (group.getUnlockMoveFX()) {
                    getMoveNode.getChildren().add(mapGroupAddNode(group, true));
                } else {
                    addMoveNode.getChildren().add(mapGroupAddNode(group, false));
                }
            }
        });
        VBox v = new VBox();
        if (getMoveNode.getChildren().size() <= 0) {
            getMoveLabel.setText("暂未有已关联组件");
        } else {
            getMoveLabel.setText("已关联组件");
        }
        v.getChildren().addAll(getMoveLabel, getMoveNode, addMoveLabel, addMoveNode);
        return v;
    }


    private Node mapImageAddNode(Image image, boolean addDel) {
        Path path = LockscreenViewPaesiiUtils.getInstance(expressionUtils).paesiiImagePath(image, lockscreenPath.toString());
        StackPane imageView = LayoutComponentUtils.getInstance().getImagePane(lockscreenPath, 100, 100, path.toString(), 100, 100, false, consumer -> {

        });
        Button addDelBtn = new Button();
        addDelBtn.setVisible(isEditRele);
        addDelBtn.setStyle("-fx-border-color: transparent;-fx-background-color: transparent");
        if (addDel) {
            addMapMoveNodeEvent(addDelBtn, image, true);
            addDelBtn.setGraphic(SVGLoadUtils.getInstance().getMinusSquareSVG(20, Color.web("#DF4444")));
            imageView.getChildren().addAll(addDelBtn);
        } else {
            addMapMoveNodeEvent(addDelBtn, image, false);
            addDelBtn.setGraphic(SVGLoadUtils.getInstance().getPlusSquareSVG(20, LockstyleMainController.buttonColor));
            imageView.getChildren().addAll(addDelBtn);
        }
        StackPane.setAlignment(addDelBtn, Pos.TOP_RIGHT);
        VBox moveVBox = new VBox();
        moveVBox.setAlignment(Pos.TOP_CENTER);
        Label label = LayoutComponentUtils.getInstance().getControlNormalLabel(path.getFileName().toString());
        label.setMaxWidth(100);
        moveVBox.getChildren().addAll(imageView, label,
                LayoutComponentUtils.getInstance().getControlNormalLabel("类型：Image"));
        HBox.setMargin(moveVBox, new Insets(0, 10, 0, 0));
        return moveVBox;
    }

    private Node mapGroupAddNode(Group group, boolean addDel) {
        StackPane stackPane = new StackPane();
        stackPane.setStyle("-fx-background-color: #bfbfbf");
        stackPane.setPrefSize(100, 100);
        stackPane.setMinSize(100, 100);
        stackPane.setMaxSize(100, 100);
        Button addDelBtn = new Button();
        addDelBtn.setVisible(isEditRele);
        addDelBtn.setStyle("-fx-border-color: transparent;-fx-background-color: transparent");
        if (addDel) {
            addMapMoveNodeEvent(addDelBtn, group, true);
            addDelBtn.setGraphic(SVGLoadUtils.getInstance().getMinusSquareSVG(20, Color.web("#DF4444")));
            stackPane.getChildren().addAll(addDelBtn);
        } else {
            addMapMoveNodeEvent(addDelBtn, group, false);
            addDelBtn.setGraphic(SVGLoadUtils.getInstance().getPlusSquareSVG(20, Color.web("#716A94")));
            stackPane.getChildren().addAll(addDelBtn);
        }
        StackPane.setAlignment(addDelBtn, Pos.TOP_RIGHT);
        VBox moveVBox = new VBox();
        moveVBox.setAlignment(Pos.TOP_CENTER);
        Label label = LayoutComponentUtils.getInstance().getControlNormalLabel(group.getName());
        label.setMaxWidth(100);
        moveVBox.getChildren().addAll(stackPane, label,
                LayoutComponentUtils.getInstance().getControlNormalLabel("类型：Group"));
        HBox.setMargin(moveVBox, new Insets(0, 10, 0, 0));
        return moveVBox;
    }

    private void addMapMoveNodeEvent(Button btn, BaseView baseView, boolean isDel) {
        btn.setOnAction(event -> {
            if (isDel) {
                baseView.setUnlockMoveFX(false);
            } else {
                baseView.setUnlockMoveFX(true);
            }
            imageAndGroupMap.put(baseView.getId(), baseView);
            updateAddGetMoveNode();
        });
    }

    private void updateAddGetMoveNode() {
        addMoveNode.getChildren().clear();
        getMoveNode.getChildren().clear();
        imageAndGroupMap.keySet().forEach(key -> {
            if (imageAndGroupMap.get(key) instanceof Image) {
                Image image = (Image) imageAndGroupMap.get(key);
                if (image.getUnlockMoveFX()) {
                    getMoveNode.getChildren().add(mapImageAddNode(image, true));
                } else {
                    addMoveNode.getChildren().add(mapImageAddNode(image, false));
                }
            } else if (imageAndGroupMap.get(key) instanceof Group) {
                Group group = (Group) imageAndGroupMap.get(key);
                if (group.getUnlockMoveFX()) {
                    getMoveNode.getChildren().add(mapGroupAddNode(group, true));
                } else {
                    addMoveNode.getChildren().add(mapGroupAddNode(group, false));
                }
            }
        });
        if (getMoveNode.getChildren().size() <= 0) {
            getMoveLabel.setText("暂未有已关联组件");
        } else {
            getMoveLabel.setText("已关联组件");
        }
    }

    private void addFieldText() {
        Label errorLabel = new Label("XML中解锁组件配置不正确");
        errorLabel.setStyle("-fx-font-size: 12;-fx-text-fill: #DF4444;");
        int spx = getXMLNum(Optional.ofNullable(baseView).flatMap(start -> Optional.ofNullable(start.getStartPoint())).flatMap(x -> Optional.ofNullable(x.getX())).orElse("-9999"));
        int spy = getXMLNum(Optional.ofNullable(baseView).flatMap(start -> Optional.ofNullable(start.getStartPoint())).flatMap(y -> Optional.ofNullable(y.getY())).orElse("-9999"));
        int spw = getXMLNum(Optional.ofNullable(baseView).flatMap(start -> Optional.ofNullable(start.getStartPoint())).flatMap(w -> Optional.ofNullable(w.getW())).orElse("-9999"));
        int sph = getXMLNum(Optional.ofNullable(baseView).flatMap(start -> Optional.ofNullable(start.getStartPoint())).flatMap(h -> Optional.ofNullable(h.getH())).orElse("-9999"));
        int epx = getXMLNum(Optional.ofNullable(baseView).flatMap(end -> Optional.ofNullable(end.getEndPoint())).flatMap(x -> Optional.ofNullable(x.getX())).orElse("-9999"));
        int epy = getXMLNum(Optional.ofNullable(baseView).flatMap(end -> Optional.ofNullable(end.getEndPoint())).flatMap(y -> Optional.ofNullable(y.getY())).orElse("-9999"));
        int epw = getXMLNum(Optional.ofNullable(baseView).flatMap(end -> Optional.ofNullable(end.getEndPoint())).flatMap(w -> Optional.ofNullable(w.getW())).orElse("-9999"));
        int eph = getXMLNum(Optional.ofNullable(baseView).flatMap(end -> Optional.ofNullable(end.getEndPoint())).flatMap(h -> Optional.ofNullable(h.getH())).orElse("-9999"));
        int posx = getXMLNum(Optional.ofNullable(baseView).flatMap(end -> Optional.ofNullable(end.getEndPoint())).flatMap(path -> Optional.ofNullable(path.getPath())).flatMap(pos -> Optional.ofNullable(pos.getPositionList().get(1))).flatMap(x -> Optional.ofNullable(x.getX())).orElse("-9999"));
        int posy = getXMLNum(Optional.ofNullable(baseView).flatMap(end -> Optional.ofNullable(end.getEndPoint())).flatMap(path -> Optional.ofNullable(path.getPath())).flatMap(pos -> Optional.ofNullable(pos.getPositionList().get(1))).flatMap(y -> Optional.ofNullable(y.getY())).orElse("-9999"));
        if (spx == -9999 || spy == -9999 || spw == -9999 || sph == -9999 || epx == -9999 || epy == -9999 || epw == -9999 || eph == -9999) {
            vBox.getChildren().add(0, errorLabel);
            isSave = false;
        } else {
            vBox.getChildren().remove(errorLabel);
            isSave = true;
        }
        spX.setText(spx + "");
        spY.setText(spy + "");
        spW.setText(spw + "");
        spH.setText(sph + "");
        epX.setText(epx + "");
        epY.setText(epy + "");
        epW.setText(epw + "");
        epH.setText(eph + "");
        if (posx > 0 && spx + posx == epx) {
            radio2.setSelected(true);
            type = UNLOCK_RIGHT;
            epL.setText(posx + "");
        } else if (posx < 0 && spx + posx - epw == epx) {
            radio4.setSelected(true);
            type = UNLOCK_LEFT;
            epL.setText(Math.abs(posx) + "");
        } else if (posy > 0 && spy + posy == epy) {
            radio3.setSelected(true);
            type = UNLOCK_DOWN;
            epL.setText(posy + "");
        } else if (posy < 0 && spy + posy - eph == epy) {
            radio1.setSelected(true);
            type = UNLOCK_UP;
            epL.setText(Math.abs(posy) + "");
        } else {
            radio1.setSelected(false);
            radio2.setSelected(false);
            radio3.setSelected(false);
            radio4.setSelected(false);
            type = "";
        }
    }

    private void addUpUnlockText() {
        if (judgeTextField(spY) && judgeTextField(epL, true) && judgeTextField(epH) && judgeTextField(spX)) {
            epY.setText((Integer.valueOf(spY.getText()) - Integer.valueOf(epL.getText()) - Integer.valueOf(epH.getText())) + "");
            epX.setText(spX.getText());
        }
    }

    private void addRightUnlockText() {
        if (judgeTextField(spX) && judgeTextField(epL, true) && judgeTextField(spY)) {
            epX.setText((Integer.valueOf(spX.getText()) + Integer.valueOf(epL.getText())) + "");
            epY.setText(spY.getText());
        }
    }

    private void addDownUnlockText() {
        if (judgeTextField(spY) && judgeTextField(epL, true) && judgeTextField(spX)) {
            epY.setText((Integer.valueOf(spY.getText()) + Integer.valueOf(epL.getText())) + "");
            epX.setText(spX.getText());
        }
    }

    private void addLeftUnlockText() {
        if (judgeTextField(spX) && judgeTextField(epL, true) && judgeTextField(epW) && judgeTextField(spY)) {
            epX.setText((Integer.valueOf(spX.getText()) - Integer.valueOf(epL.getText()) - Integer.valueOf(epW.getText())) + "");
            epY.setText(spY.getText());
        }
    }

    private void addRadioEvent() {
        radio1.selectedProperty().addListener((obj, oldValue, newValue) -> {
            if (newValue) {
                type = UNLOCK_UP;
                addUpUnlockText();
            }
        });
        radio2.selectedProperty().addListener((obj, oldValue, newValue) -> {
            if (newValue) {
                type = UNLOCK_RIGHT;
                addRightUnlockText();
            }
        });
        radio3.selectedProperty().addListener((obj, oldValue, newValue) -> {
            if (newValue) {
                type = UNLOCK_DOWN;
                addDownUnlockText();
            }
        });
        radio4.selectedProperty().addListener((obj, oldValue, newValue) -> {
            if (newValue) {
                type = UNLOCK_LEFT;
                addLeftUnlockText();
            }
        });
    }

    private void addFieldEvent() {
        spX.textProperty().addListener(event -> {
            switch (type) {
                case UNLOCK_UP:
                case UNLOCK_DOWN:
                    addUpUnlockText();
                    break;
                case UNLOCK_RIGHT:
                    addRightUnlockText();
                    break;
                case UNLOCK_LEFT:
                    addLeftUnlockText();
                    break;
                default:
                    judgeTextField(spX);
                    break;
            }
        });
        spY.textProperty().addListener(event -> {
            switch (type) {
                case UNLOCK_UP:
                    addUpUnlockText();
                    break;
                case UNLOCK_DOWN:
                    addDownUnlockText();
                    break;
                case UNLOCK_RIGHT:
                case UNLOCK_LEFT:
                    addRightUnlockText();
                    break;
                default:
                    judgeTextField(spY);
                    break;
            }
        });
        spW.textProperty().addListener(event -> {
            if (judgeTextField(spW)) {
                epW.setText(spW.getText());
            }
        });
        spH.textProperty().addListener(event -> {
            if (judgeTextField(spH)) {
                epH.setText(spH.getText());
            }
        });
        epX.textProperty().addListener(event -> {
            judgeTextField(epX);
        });
        epY.textProperty().addListener(event -> {
            judgeTextField(epY);
        });
        epW.textProperty().addListener(event -> {
            switch (type) {
                case UNLOCK_UP:
                case UNLOCK_DOWN:
                case UNLOCK_RIGHT:
                    judgeTextField(epW);
                    break;
                case UNLOCK_LEFT:
                    addLeftUnlockText();
                    break;
                default:
                    judgeTextField(epW);
                    break;
            }
        });
        epH.textProperty().addListener(event -> {
            switch (type) {
                case UNLOCK_UP:
                    addUpUnlockText();
                    break;
                case UNLOCK_DOWN:
                case UNLOCK_LEFT:
                case UNLOCK_RIGHT:
                    judgeTextField(epH);
                    break;
                default:
                    judgeTextField(epH);
                    break;
            }
        });
        epL.textProperty().addListener(event -> {
            switch (type) {
                case UNLOCK_UP:
                    addUpUnlockText();
                    break;
                case UNLOCK_DOWN:
                    addDownUnlockText();
                    break;
                case UNLOCK_LEFT:
                    addLeftUnlockText();
                    break;
                case UNLOCK_RIGHT:
                    addRightUnlockText();
                    break;
                default:
                    judgeTextField(epL, true);
                    break;
            }
        });
    }

    private int getXMLNum(String text) {
        return Optional.of(text.trim())
                .filter(str -> str.length() > 0 && expressionUtils.parseExpr(str) != -9999)
                .map(str -> (int) expressionUtils.parseExpr(str))
                .orElse(-9999);
    }

    private boolean judgeTextField(TextField textField) {
        return judgeTextField(textField, false);
    }

    private boolean judgeTextField(TextField textField, boolean tag) {
        if (tag) {
            if (textField.getText().length() <= 0 || expressionUtils.parseExpr(textField.getText()) == -9999) {
                textField.setId("textfield-error");
                isSave = false;
                return false;
            } else {
                if (Integer.valueOf(textField.getText()) <= 0) {
                    textField.setId("textfield-error");
                    isSave = false;
                    return false;
                } else {
                    textField.setId("diy-textfield");
                    isSave = true;
                    return true;
                }
            }
        } else {
            if (textField.getText().length() <= 0 || expressionUtils.parseExpr(textField.getText()) == -9999) {
                textField.setId("textfield-error");
                isSave = false;
                return false;
            } else {
                textField.setId("diy-textfield");
                isSave = true;
                return true;
            }
        }
    }

    @Override
    public Node getCacheLockscreenControl() {
        lockscreenMap = lockstyleShowUtils.getLockscreen().getLockScreenViewMap();
        lockscreenMap.keySet().forEach(key -> {
            if (lockscreenMap.get(key) instanceof Image) {
                if (!"bg".equals(lockscreenMap.get(key).getName())) {
                    imageAndGroupMap.put(key, lockscreenMap.get(key));
                }
            } else if (lockscreenMap.get(key) instanceof Group) {
                imageAndGroupMap.put(key, lockscreenMap.get(key));
            }
        });
        updateAddGetMoveNode();
        return vBox;
    }

    @Override
    public void updateView() {

    }
}
